package com.atguigu.java2;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 一、构造器引用
 *
 *         和方法引用类似，函数式接口的抽象方法的形参列表和构造器的形参列表一致
 *         抽象方法的返回值类型即为构造器所属类的类型
 *
 *
 * 二、数组引用
 *         大家可以把数组看做是一个特殊的类，则写法与构造器引用一致
 *
 * Created by shkstart
 */
public class ConstructorRefTest {
	//构造器引用
    //Supplier中的T get()
    @Test
    public void test1(){
        Supplier<Employee> supplier = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(supplier.get());
        System.out.println("***********************************");

        Supplier<Employee> supplier2 = () ->new Employee();
        System.out.println(supplier2.get());

        System.out.println("***********************************");

        Supplier<Employee> supplier3 = Employee::new;
        System.out.println(supplier3.get());

    }

	//Function中的R apply(T t)
    @Test
    public void test2(){
        Function<Integer,Employee> function = id -> new Employee(id);
        System.out.println(function.apply(1001));
        System.out.println("***********************************");
        Function<Integer,Employee> function2 = Employee::new;
        System.out.println(function2.apply(1002));
    }

	//BiFunction中的R apply(T t,U u)
    @Test
    public void test3(){
        BiFunction<Integer,String,Employee> biFunction = (id,name) -> new Employee(id,name);
        System.out.println(biFunction.apply(1001, "Jerry"));

        System.out.println("***********************************");

        BiFunction<Integer,String,Employee> biFunction2 = Employee::new;
        System.out.println(biFunction2.apply(1002, "Jerry"));

    }

	//数组引用
    //Function中的R apply(T t)
    @Test
    public void test4(){
        Function<Integer,String[]> function = length ->new String[length];
        String[] arr = function.apply(5);
        System.out.println(Arrays.toString(arr));
        System.out.println("*******************************************");
        Function<Integer,String[]> function2 = String[]::new;
        String[] arr2 = function2.apply(10);
        System.out.println(Arrays.toString(arr2));
    }
}
